<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="google-site-verification" content="VGgQeH6NiuAKspyCFT7dqUNmNhg6RJoMYQXErdy0jgE" />
  <meta name="baidu-site-verification" content="code-yNmdsKJ9GP" />
  
  
  <meta name="keywords" content="Elasticsearch,elasticsearch,搭建，环境搭建,">
  
  
  <meta name="description" content="Elasticsearch 环境搭建">
  
  <title>
    3.Elasticsearch 基本操作 |
    
    思远程序
  </title>
  <!-- Icon -->
  
    <link rel="shortcut icon" href="/favicon.ico">
    
  
<link rel="stylesheet" href="/css/style.css">

  
  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<script src="/js/pace.min.js"></script>

<meta name="generator" content="Hexo 6.2.0"></head>

<body>
  <main class="content">
    <section class="outer">
  <article id="post-3. Elasticsearch 基本操作" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
      

<h1 class="article-title" itemprop="name">
  3.Elasticsearch 基本操作
</h1>



    </header>
    

    
    <div class="article-meta">
      <a href="/2022/07/23/3.%20Elasticsearch%20%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/" class="article-date">
  <time datetime="2022-07-23T04:00:00.000Z" itemprop="datePublished">2022-07-23</time>
</a>
      
<div class="article-category">
  <a class="article-category-link" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E6%A1%86%E6%9E%B6/">分布式框架</a>
</div>

    </div>
    

    
    
<div class="tocbot"></div>

    

    <div class="article-entry" itemprop="articleBody">
      
      
      
      <h3 id="Rest-风格"><a href="#Rest-风格" class="headerlink" title="Rest 风格"></a>Rest 风格</h3><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">POST /uri 创建</span><br><span class="line">DELETE /uri/xxx 删除</span><br><span class="line">PUT /uri/xxx 更新或创建</span><br><span class="line">GET /uri/xxx 查看</span><br></pre></td></tr></table></figure>

<span id="more"></span>

<h3 id="索引操作"><a href="#索引操作" class="headerlink" title="索引操作"></a>索引操作</h3><h4 id="创建索引"><a href="#创建索引" class="headerlink" title="创建索引"></a>创建索引</h4><p>索引命名必须小写，不能以下划线开头<br>格式：PUT &#x2F;索引名称</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"># 创建索引</span><br><span class="line">PUT /es_db</span><br><span class="line"></span><br><span class="line"># 创建索引时可以设置分片数和副本数</span><br><span class="line">PUT /es_db</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;settings&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;number_of_shards&quot;</span><span class="punctuation">:</span> <span class="number">3</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;number_of_replicas&quot;</span><span class="punctuation">:</span> <span class="number">2</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"># 修改索引配置</span><br><span class="line">PUT /es_db/_settings</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;settings&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;number_of_replicas&quot;</span><span class="punctuation">:</span> <span class="number">1</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="查询所有"><a href="#查询所有" class="headerlink" title="查询所有"></a>查询所有</h4><p>格式：GET &#x2F;所有名称</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 查看创建好的索引</span><br><span class="line">GET /es_db</span><br><span class="line"></span><br><span class="line"># es_db是否存在</span><br><span class="line">HEAD /es_db</span><br></pre></td></tr></table></figure>
<h4 id="删除所有"><a href="#删除所有" class="headerlink" title="删除所有"></a>删除所有</h4><p>格式: DELETE &#x2F;索引名称</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 删除所有</span><br><span class="line">DELETE /es_db</span><br></pre></td></tr></table></figure>
<h3 id="文档操作"><a href="#文档操作" class="headerlink" title="文档操作"></a>文档操作</h3><h4 id="添加文档"><a href="#添加文档" class="headerlink" title="添加文档"></a>添加文档</h4><p>格式: [PUT | POST] &#x2F;索引名称&#x2F;[_doc | _create ]&#x2F;id</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"># 创建文档，指定id</span><br><span class="line">PUT /es_db/_doc/<span class="number">1</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;张三&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;sex&quot;</span><span class="punctuation">:</span><span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span><span class="number">25</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;address&quot;</span><span class="punctuation">:</span><span class="string">&quot;广州天河公园&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;remark&quot;</span><span class="punctuation">:</span><span class="string">&quot;java developer&quot;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"></span><br><span class="line"># 创建文档，ES 生成id</span><br><span class="line">POST /es_db/_doc</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;张三&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;sex&quot;</span><span class="punctuation">:</span><span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span><span class="number">25</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;address&quot;</span><span class="punctuation">:</span><span class="string">&quot;广州天河公园&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;remark&quot;</span><span class="punctuation">:</span><span class="string">&quot;java developer&quot;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>
<p>注意：POST和PUT都能起到创建&#x2F;更新的作用，PUT需要对一个具体的资源进行操作也就是 要确定id才能进行更新&#x2F;创建，而POST是可以针对整个资源集合进行操作的，如果不写id就 由ES生成一个唯一id进行创建新文档，如果填了id那就针对这个id的文档进行创建&#x2F;更新。<br><img src="https://cdn.nlark.com/yuque/0/2022/png/1445568/1656927482590-573cc0da-b161-40c1-94fc-089cec4ba20e.png#clientId=u75638758-1cd0-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=501&id=u7283e9a9&margin=%5Bobject%20Object%5D&name=image.png&originHeight=876&originWidth=530&originalType=binary&ratio=1&rotation=0&showTitle=false&size=110755&status=done&style=stroke&taskId=u359cc279-95b9-4470-b1db-a9362336377&title=&width=303" alt="image.png"><br>create: 如果id存在会失败；<br><img src="https://cdn.nlark.com/yuque/0/2022/png/1445568/1656927617116-5d83f6c5-411d-4d61-b942-3ace2d5c6f14.png#clientId=u75638758-1cd0-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=200&id=u85e53ecf&margin=%5Bobject%20Object%5D&name=image.png&originHeight=399&originWidth=1410&originalType=binary&ratio=1&rotation=0&showTitle=false&size=113128&status=done&style=stroke&taskId=uc402d66a-4586-4c9f-b14d-d3bfa2825aa&title=&width=705" alt="image.png"></p>
<h4 id="修改文档"><a href="#修改文档" class="headerlink" title="修改文档"></a>修改文档</h4><ul>
<li>全量更新，整个json都会替换，格式: [PUT | POST] &#x2F;索引名称&#x2F;_doc&#x2F;id</li>
</ul>
<p>如果文档存在，现有文档会被删除，新的文档会被索引</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># 全量更新，替换整个json</span><br><span class="line">PUT /es_db/_doc/<span class="number">1</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;张三&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;sex&quot;</span><span class="punctuation">:</span><span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span><span class="number">25</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"># 查询文档</span><br><span class="line">GET /es_db/_doc/<span class="number">1</span></span><br></pre></td></tr></table></figure>

<p><img src="https://cdn.nlark.com/yuque/0/2022/png/1445568/1656927755557-73e77241-d6c6-4de9-ab3a-c859f09fcca4.png#clientId=u2a936614-a736-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=211&id=u54fb7f43&margin=%5Bobject%20Object%5D&name=image.png&originHeight=309&originWidth=901&originalType=binary&ratio=1&rotation=0&showTitle=false&size=60046&status=done&style=stroke&taskId=ubc6003ad-def5-41e2-a756-7fef786f5f0&title=&width=616.5" alt="image.png"></p>
<ul>
<li>使用_update部分更新，格式: POST &#x2F;索引名称&#x2F;_update&#x2F;id</li>
</ul>
<p>update不会删除原来的文档，而是实现真正的数据更新</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 使用_update部分更新</span><br><span class="line">POST /es_db/_update/<span class="number">1</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;doc&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span> <span class="number">28</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span>`</span><br></pre></td></tr></table></figure>

<ul>
<li><p>使用 _update_by_query 更新文档</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 更新符合条件的文档</span><br><span class="line">POST /es_db/_update_by_query</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;query&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;match&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">      <span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">1</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line">  <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;script&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;source&quot;</span><span class="punctuation">:</span><span class="string">&quot;ctx._source.age = 30&quot;</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.nlark.com/yuque/0/2022/png/1445568/1656928188777-75bb0648-61ef-461b-9268-7b9ff0d785ad.png#clientId=u2a936614-a736-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=253&id=u0a7bfd37&margin=%5Bobject%20Object%5D&name=image.png&originHeight=284&originWidth=349&originalType=binary&ratio=1&rotation=0&showTitle=false&size=28074&status=done&style=stroke&taskId=uf0db5e62-69ee-4e33-81ac-6b518e101b9&title=&width=310.5" alt="image.png"></p>
</li>
<li><p>并发场景下修改文档</p>
</li>
</ul>
<p>_seq_no和_primary_term是对_version的优化，7.X版本的ES默认使用这种方式控制版本，所以当在高并发环境下使用乐观锁机制修改文档时，要带上当前文 档的_seq_no和_primary_term进行更新：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 如果版本号不对，会抛出版本冲突异常</span><br><span class="line">POST /es_db/_doc/<span class="number">1</span>?if_seq_no=<span class="number">5</span>&amp;if_primary_term=<span class="number">1</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;zhangsan&quot;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.nlark.com/yuque/0/2022/png/1445568/1656928430252-aa64af81-8e9e-4ada-ac28-dfc6f3830a6a.png#clientId=u2a936614-a736-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=198&id=u5e967b53&margin=%5Bobject%20Object%5D&name=image.png&originHeight=395&originWidth=1787&originalType=binary&ratio=1&rotation=0&showTitle=false&size=115339&status=done&style=stroke&taskId=ub402fe9c-8643-4ef8-8bb4-cd6e97a721c&title=&width=893.5" alt="image.png"></p>
<h4 id="查询文档"><a href="#查询文档" class="headerlink" title="查询文档"></a>查询文档</h4><ul>
<li><p>根据id查询文档，格式: GET &#x2F;索引名称&#x2F;_doc&#x2F;id</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 查询文档</span><br><span class="line">GET /es_db/_doc/<span class="number">1</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>条件查询 _search，格式： &#x2F;索引名称&#x2F;_doc&#x2F;_search</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /es_db/_doc/_search</span><br></pre></td></tr></table></figure></li>
</ul>
<p>ES Search API提供了两种条件查询搜索方式： </p>
<ul>
<li><p>REST风格的请求URI，直接将参数带过去；</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"># 条件查询<span class="punctuation">,</span> 如要查询age等于<span class="number">28</span>岁的</span><br><span class="line">GET /es_db/_doc/_search?q=age<span class="punctuation">:</span><span class="number">28</span></span><br><span class="line"></span><br><span class="line"># 范围查询<span class="punctuation">,</span> 如要查询age在<span class="number">25</span>至<span class="number">26</span>岁之间的 _search?q=***<span class="punctuation">[</span>** TO **<span class="punctuation">]</span> 注意<span class="punctuation">:</span> TO 必须为大写</span><br><span class="line">GET /es_db/_doc/_search?q=age<span class="punctuation">[</span><span class="number">25</span> TO <span class="number">26</span><span class="punctuation">]</span></span><br><span class="line"></span><br><span class="line"># 查询年龄小于等于<span class="number">28</span>岁的 <span class="punctuation">:</span>&lt;=</span><br><span class="line">GET /es_db/_doc/_search?q=age<span class="punctuation">:</span>&lt;=<span class="number">28</span></span><br><span class="line"></span><br><span class="line"># 查询年龄大于<span class="number">28</span>前的 <span class="punctuation">:</span>&gt;</span><br><span class="line">GET /es_db/_doc/_search?q=age<span class="punctuation">:</span>&gt;<span class="number">28</span></span><br><span class="line"></span><br><span class="line">#分页查询 from=*&amp;size=*</span><br><span class="line">GET /es_db/_doc/_search?&amp;sort=age<span class="punctuation">:</span>desc&amp;from=<span class="number">0</span>&amp;size=<span class="number">2</span></span><br><span class="line"></span><br><span class="line"># 对查询结果值输出指定字段 _source=字段<span class="punctuation">,</span>字段</span><br><span class="line">GET /es_db/_doc/_search?_source=name<span class="punctuation">,</span>age</span><br><span class="line"></span><br><span class="line"># 对查询结果排序 sort=字段<span class="punctuation">:</span>desc/asc</span><br><span class="line">GET /es_db/_doc/_search?sort=age<span class="punctuation">:</span>asc</span><br></pre></td></tr></table></figure>
</li>
<li><p>封装到request body中，这种方式可以定义更加易读的JSON格式；</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># 查询 age=<span class="number">28</span></span><br><span class="line">GET /es_db/_doc/_search</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;query&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;match&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">      <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span><span class="number">28</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="删除文档"><a href="#删除文档" class="headerlink" title="删除文档"></a>删除文档</h4><p>格式: DELETE &#x2F;索引名称&#x2F;_doc&#x2F;id</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DELETE /es_db/_doc/<span class="number">1</span></span><br></pre></td></tr></table></figure>
<h3 id="文档批量操作"><a href="#文档批量操作" class="headerlink" title="文档批量操作"></a>文档批量操作</h3><p>批量操作可以减少网络连接所产生的开销，提升性能</p>
<ul>
<li>支持在一次API调用中，对不同的索引进行操作；</li>
<li>可以在URI中指定Index，也可以在请求的Payload中进行；</li>
<li>操作中单条操作失败，并不会影响其他操作；</li>
<li>返回结果包括了每一条操作执行的结果；</li>
</ul>
<h4 id="批量写入"><a href="#批量写入" class="headerlink" title="批量写入"></a>批量写入</h4><p>批量对文档进行写操作是通过_bulk的API来实现的</p>
<ul>
<li>请求方式：POST；</li>
<li>请求地址：_bulk；</li>
<li>请求参数：通过_bulk操作文档，一般至少有两行参数(或偶数行参数)；<ul>
<li>第一行参数为指定操作的类型及操作的对象(index,type和id)；</li>
<li>第二行参数才是操作的数据；</li>
</ul>
</li>
</ul>
<p>参数类似于：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;actionName&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;indexName&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;typeName&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="string">&quot;id&quot;</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;field1&quot;</span><span class="punctuation">:</span><span class="string">&quot;value1&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;field2&quot;</span><span class="punctuation">:</span><span class="string">&quot;value2&quot;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>
<p>actionName：表示操作类型，主要有create,index,delete和update</p>
<h4 id="批量创建文档-create"><a href="#批量创建文档-create" class="headerlink" title="批量创建文档 create"></a>批量创建文档 create</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 批量创建文档</span><br><span class="line">POST _bulk</span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;create&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">,</span><span class="attr">&quot;title&quot;</span><span class="punctuation">:</span><span class="string">&quot;fox老师&quot;</span><span class="punctuation">,</span><span class="attr">&quot;content&quot;</span><span class="punctuation">:</span><span class="string">&quot;fox老师666&quot;</span><span class="punctuation">,</span><span class="attr">&quot;tags&quot;</span><span class="punctuation">:</span><span class="punctuation">[</span><span class="string">&quot;java&quot;</span><span class="punctuation">,</span><span class="string">&quot;面向对象&quot;</span><span class="punctuation">]</span><span class="punctuation">,</span><span class="attr">&quot;create_time&quot;</span><span class="punctuation">:</span><span class="number">1554015482530</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;create&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">4</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="number">4</span><span class="punctuation">,</span><span class="attr">&quot;title&quot;</span><span class="punctuation">:</span><span class="string">&quot;mark老师&quot;</span><span class="punctuation">,</span><span class="attr">&quot;content&quot;</span><span class="punctuation">:</span><span class="string">&quot;mark老师NB&quot;</span><span class="punctuation">,</span><span class="attr">&quot;tags&quot;</span><span class="punctuation">:</span><span class="punctuation">[</span><span class="string">&quot;java&quot;</span><span class="punctuation">,</span><span class="string">&quot;面向对象&quot;</span><span class="punctuation">]</span><span class="punctuation">,</span><span class="attr">&quot;create_time&quot;</span><span class="punctuation">:</span><span class="number">1554015482530</span><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>
<h4 id="普通创建或全量替换-index"><a href="#普通创建或全量替换-index" class="headerlink" title="普通创建或全量替换 index"></a>普通创建或全量替换 index</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">POST _bulk</span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;index&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">,</span><span class="attr">&quot;title&quot;</span><span class="punctuation">:</span><span class="string">&quot;图灵徐庶老师&quot;</span><span class="punctuation">,</span><span class="attr">&quot;content&quot;</span><span class="punctuation">:</span><span class="string">&quot;图灵学院徐庶老师666&quot;</span><span class="punctuation">,</span><span class="attr">&quot;tags&quot;</span><span class="punctuation">:</span><span class="punctuation">[</span><span class="string">&quot;java&quot;</span><span class="punctuation">,</span><span class="string">&quot;面向对象&quot;</span><span class="punctuation">]</span><span class="punctuation">,</span><span class="attr">&quot;create_time&quot;</span><span class="punctuation">:</span><span class="number">1554015482530</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;index&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">4</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="number">4</span><span class="punctuation">,</span><span class="attr">&quot;title&quot;</span><span class="punctuation">:</span><span class="string">&quot;图灵诸葛老师&quot;</span><span class="punctuation">,</span><span class="attr">&quot;content&quot;</span><span class="punctuation">:</span><span class="string">&quot;图灵学院诸葛老师NB&quot;</span><span class="punctuation">,</span><span class="attr">&quot;tags&quot;</span><span class="punctuation">:</span><span class="punctuation">[</span><span class="string">&quot;java&quot;</span><span class="punctuation">,</span><span class="string">&quot;面向对象&quot;</span><span class="punctuation">]</span><span class="punctuation">,</span><span class="attr">&quot;create_time&quot;</span><span class="punctuation">:</span><span class="number">1554015482530</span><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>如果原文档不存在，则是创建</li>
<li>如果原文档存在，则是替换(全量修改原文档) ；</li>
</ul>
<h4 id="批量删除-delete"><a href="#批量删除-delete" class="headerlink" title="批量删除 delete"></a>批量删除 delete</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 批量删除 delete</span><br><span class="line">POST _bulk</span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;delete&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;delete&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">4</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>
<h4 id="批量修改-update"><a href="#批量修改-update" class="headerlink" title="批量修改 update"></a>批量修改 update</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 批量修改 update</span><br><span class="line">POST _bulk</span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;update&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;doc&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;title&quot;</span><span class="punctuation">:</span><span class="string">&quot;ES大法必修内功&quot;</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;update&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">4</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;doc&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;create_time&quot;</span><span class="punctuation">:</span><span class="number">1554018421008</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>
<h4 id="组合应用"><a href="#组合应用" class="headerlink" title="组合应用"></a>组合应用</h4><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 组合应用</span><br><span class="line">POST _bulk</span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;create&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">,</span><span class="attr">&quot;title&quot;</span><span class="punctuation">:</span><span class="string">&quot;fox老师&quot;</span><span class="punctuation">,</span><span class="attr">&quot;content&quot;</span><span class="punctuation">:</span><span class="string">&quot;fox老师666&quot;</span><span class="punctuation">,</span><span class="attr">&quot;tags&quot;</span><span class="punctuation">:</span><span class="punctuation">[</span><span class="string">&quot;java&quot;</span><span class="punctuation">,</span><span class="string">&quot;面向对 象&quot;</span><span class="punctuation">]</span><span class="punctuation">,</span><span class="attr">&quot;create_time&quot;</span><span class="punctuation">:</span><span class="number">1554015482530</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;delete&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">3</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;update&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;_index&quot;</span><span class="punctuation">:</span><span class="string">&quot;article&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_type&quot;</span><span class="punctuation">:</span><span class="string">&quot;_doc&quot;</span><span class="punctuation">,</span><span class="attr">&quot;_id&quot;</span><span class="punctuation">:</span><span class="number">4</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span><span class="attr">&quot;doc&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span><span class="attr">&quot;create_time&quot;</span><span class="punctuation">:</span><span class="number">1554018421008</span><span class="punctuation">,</span><span class="attr">&quot;title&quot;</span><span class="punctuation">:</span><span class="string">&quot;siyuan&quot;</span><span class="punctuation">&#125;</span><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://siyit.gitee.io/2022/07/23/3.%20Elasticsearch%20%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/" data-id="cl6bukbt6000msu6fejalh2lh" class="article-share-link">
        分享
      </a>
      
<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Elasticsearch/" rel="tag">Elasticsearch</a></li></ul>

    </footer>

  </div>

  
  
<nav class="article-nav">
  
  <a href="/2022/07/23/4.%20Elasticsearch%20Mapping%E5%92%8CTemplate%E6%93%8D%E4%BD%9C/" class="article-nav-link">
    <strong class="article-nav-caption">前一篇</strong>
    <div class="article-nav-title">
      
      4. Elasticsearch Mapping和Template操作
      
    </div>
  </a>
  
  
  <a href="/2022/07/23/2.%20Elasticsearch%20%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/" class="article-nav-link">
    <strong class="article-nav-caption">后一篇</strong>
    <div class="article-nav-title">2.Elasticsearch 快速入门</div>
  </a>
  
</nav>

  

  
  
<div class="vcomments" id="vcomments"></div>

<script src="https://unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  new Valine({
    el: '#vcomments',
    appId: 'A7Ny5JW4l2XoShLWoQfpND2b-gzGzoHsz',
    appKey: 'lTbAjSoXEDQETkbAcE4zpYpu',
    notify: 'true',
    verify: 'true',
    avatar: 'identicon',
    pageSize: '10',
    placeholder: '请输入...'
  })
</script>

  
  

</article>
</section>
    <footer class="footer">
  <div class="outer">
    <div class="float-right">
      <ul class="list-inline">
  
  <li><i class="fe fe-smile-alt"></i> <span id="busuanzi_value_site_uv"></span></li>
  
  <li><i class="fe fe-bookmark"></i> <span id="busuanzi_value_page_pv"></span></li>
  
</ul>
    </div>
    <ul class="list-inline">
      <li>思远程序 &copy; 2022</li>
      
        <li></li>
      
      <li>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></li>
      <li>theme  <a target="_blank" rel="noopener" href="https://github.com/zhwangart/hexo-theme-ocean">Ocean</a></li>
    </ul>
  </div>
</footer>
  </main>
  <aside class="sidebar">
    <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/hexo.svg" alt="思远程序"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/recommend">推荐</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/gallery">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/favorites">收藏</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="搜索">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      <div class="totop" id="totop">
  <i class="fe fe-rocket"></i>
</div>
    </li>
    <li class="nav-item">
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="fe fe-feed"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
  </aside>
  
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script src="/fancybox/jquery.fancybox.min.js"></script>





<script src="/js/tocbot.min.js"></script>


<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
  });
</script>



<script src="/js/ocean.js"></script>

</body>

</html>